import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/views/Layout.vue";
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [
  //  ----登录---
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/Login.vue'),
    beforeEnter: (to, from, next) => {
      const login = Cookies.get('login')
      if(login){
        router.push('/')
      }
      next();
    }
  },
  {
    path: '/signup',
    name: 'SignUp',
    component: () => import('@/views/login/SignUp.vue')
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    beforeEnter: (to, from, next) => {
      const login = Cookies.get('login')
      if(!login){
        router.push('/login')
      }
      next();
    },
    children:[
      //  ----主页----
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/home/Home.vue')
      },
      //  ----借阅----
      {
        path: 'bookBorrow',
        name: 'BookBorrow',
        component: () => import('@/views/borrow/BookBorrow.vue')
      },
      {
        path: 'borrowRecord',
        name: 'BorrowRecord',
        component: () => import('@/views/borrow/BorrowRecord.vue')
      },
      //  ----用户----
      {
        path: 'addUser',
        name: 'AddUser',
        component: () => import('@/views/user/AddUser.vue')
      },
      {
        path: 'listUser',
        name: 'ListUser',
        component: () => import('@/views/user/ListUser.vue')
      },
      {
        path: 'editUser',
        name: 'EditUser',
        component: () => import('@/views/user/EditUser.vue')
      },
      //  ----管理员----
      {
        path: 'addAdmin',
        name: 'AddAdmin',
        component: () => import('@/views/admin/AddAdmin.vue')
      },
      {
        path: 'listAdmin',
        name: 'ListAdmin',
        component: () => import('@/views/admin/ListAdmin.vue')
      },
      {
        path: 'editAdmin',
        name: 'EditAdmin',
        component: () => import('@/views/admin/EditAdmin.vue')
      },
      //  ----图书----
      {
        path: 'addBook',
        name: 'AddBook',
        component: () => import('@/views/book/AddBook.vue')
      },
      {
        path: 'listBook',
        name: 'ListBook',
        component: () => import('@/views/book/ListBook.vue')
      },
      {
        path: 'bookBorrowRecord',
        name: 'BookBorrowRecord',
        component: () => import('@/views/book/BookBorrowRecord.vue')
      },
      {
        path: 'editBook',
        name: 'EditBook',
        component: () => import('@/views/book/EditBook.vue')
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
